﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Core;
using RayDen.Library.Core.Primitives;
using RayDen.Library.Entity.Scene;
using RayDen.RayEngine.Core.Interface;

namespace RayDen.RayEngine.Samplers
{
    public class VolumeSampler
    {

        private RayEngineScene scene;


        public VolumeSampler(RayEngineScene sc)
        {
            this.scene = sc;
        }

        static VolumeMaterialInfo Default = new VolumeMaterialInfo()
        {
            Absorbtion = new RgbSpectrum(0.01f),
            Inscattering = new RgbSpectrum(0.007f),
            //Outscattering = new RgbSpectrum(0.075f),
            MediumInfo = new MediumInfo()
            {
                IoR = 1.7f,
                IndexOfRefraction = new Vector(1.7f),
                Attenuation = 1f
            },
            Density = new SolidTextureInfo() { Expression = "noise" }
            //new SolidTextureInfo() { Expression = "$saw<p>"}
        };

        public float SampleDistance(float u0, float extinct)
        {
            return (float)Math.Log(1f - u0) / extinct;
        }

        public VolumeMaterialInfo GetMaterialInfo(int materialId)
        {
            //if (scene.VolumeMats.Count - 1 < materialId)
            if (!scene.VolumeMats.Any())
                return Default;
            return scene.VolumeMats[0];
        }

        public static bool IsVolume(ITriangleMesh mesh)
        {
            return mesh.MeshName.NameContains("volume","_volume","volume_");
        }
    }
}
